<template>
	<div id="newScore">
		<div class="breadcrumb">
			<img src="../../../../static/img/appoinment_1.png" class="head_img">
			<span class="title" @click="cancel()">成绩管理</span>/ 新增
		</div>

		<div class="list">
			<div class="list_head">
				<span class="head_name">新增成绩</span>
				<el-button class="save" type="primary" @click="save()">保存</el-button>
				<el-button class="cancel" type="primary" @click="cancel()">返回</el-button>
			</div>
			<el-form ref="scoreForm" :model="scoreForm" :rules="rules" label-width="160px" :inline="true">
				<div class="mytitle">基本信息</div>
				<el-form-item label="学号" prop="campusId" required>
					<el-input v-model="scoreForm.campusId" @blur="completeInfo()" placeholder="请输入学号" class="el-input"></el-input>
				</el-form-item>
				<el-form-item label="姓名" required>
					<el-input v-model="scoreForm.name" class="el-input" disabled></el-input>
				</el-form-item>
				<br>
				<el-form-item label="性别" required>
					<el-input v-model="scoreForm.gender" class="el-input" disabled></el-input>
				</el-form-item>
				<el-form-item label="年级" required>
					<el-input v-model="scoreForm.grade" class="el-input" disabled></el-input>
				</el-form-item>
				<br>
				<el-form-item label="院系" required>
					<el-input v-model="scoreForm.college" class="el-input" disabled></el-input>
				</el-form-item>
				<el-form-item label="体测名称" required>
					<el-select v-model="scoreForm.testId" class="el-input" @change="completeInfo()">
						<el-option v-for='item in tests' :label='item.name' :value='item.physicalId' :key='item.physicalId'></el-option>
					</el-select>
				</el-form-item>
				<br>
				<div class="mytitle">体测成绩</div>
				<el-form-item label="身高" prop="height" required>
					<el-input v-model.number="scoreForm.height" placeholder="请输入测试成绩" class="el-input"></el-input>厘米
				</el-form-item>
				<el-form-item label="体重" prop="weight" required>
					<el-input v-model="scoreForm.weight" placeholder="请输入测试成绩" class="el-input"></el-input>千克
				</el-form-item>
				<br>
				<el-form-item label="肺活量" prop="feihuoliang" required>
					<el-input v-model.number="scoreForm.feihuoliang" placeholder="请输入测试成绩" class="el-input"></el-input>毫升
				</el-form-item>
				<el-form-item label="50米跑" prop="shortRun" required>
					<el-input v-model="scoreForm.shortRun" placeholder="请输入测试成绩" class="el-input"></el-input>秒
				</el-form-item>
				<br>
				<el-form-item label="坐位体前屈" prop="qianqu" required>
					<el-input v-model.number="scoreForm.qianqu" placeholder="请输入测试成绩" class="el-input"></el-input>厘米
				</el-form-item>
				<el-form-item label="立定跳远" prop="tiaoyuan" required>
					<el-input v-model.number="scoreForm.tiaoyuan" placeholder="请输入测试成绩" class="el-input"></el-input>厘米
				</el-form-item>
				<br>
				<el-form-item label="1000米/800米跑" prop="longRun_min" required>
					<el-input v-model.number="scoreForm.longRun_min" class="mytime"></el-input>分
				</el-form-item>
				<el-form-item prop="longRun_sec" required>
					<el-input v-model="scoreForm.longRun_sec" class="mytime"></el-input>秒
				</el-form-item>
				<el-form-item label="引体向上/仰卧起坐" prop="up" required>
					<el-input v-model.number="scoreForm.up" placeholder="请输入测试成绩" class="el-input"></el-input>次
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script>
	import { mapState, mapActions } from "vuex";

	export default {
		data() {
			let checkNumber = (rule, value, callback) => {
				if (value) {
					if (isNaN(value)) {
						callback(new Error("请输入数字"));
					} else {
						if (value <= 0) {
							callback(new Error("请输入正数"));
						} else {
							callback();
						}
					}
				} else {
					callback(new Error("请输入测试成绩"));
				}
			};

			let checkInt = (rule, value, callback) => {
				if (value) {
					if (!Number.isInteger(value * 1)) {
						callback(new Error("请输入整数"));
					} else {
						if (value * 1 <= 0) {
							callback(new Error("请输入正数"));
						} else {
							callback();
						}
					}
				} else {
					callback(new Error("请输入测试成绩"));
				}
			};

			let checkWeight = (rule, value, callback) => {
				if (value) {
					if (!Number.isInteger(value * 10)) {
						callback(new Error("请输入正确的测试成绩"));
					} else {
						callback();
					}
				} else {
					callback(new Error("请输入测试成绩"));
				}
			};

			let checkQianqu = (rule, value, callback) => {
				if (value) {
					if (!Number.isInteger(value * 10)) {
						callback(new Error("请输入正确的测试成绩"));
					} else {
						callback();
					}
				} else {
					callback(new Error("请输入测试成绩"));
				}
			};

			let checkShortRun = (rule, value, callback) => {
				if (value) {
					if (!Number.isInteger(value * 10)) {
						callback(new Error("请输入正确的测试成绩"));
					} else {
						callback();
					}
				} else {
					callback(new Error("请输入测试成绩"));
				}
			};

			let checkFHL = (rule, value, callback) => {
				if (value) {
					callback();
				} else {
					callback(new Error("请输入测试成绩"));
				}
			};

			let checkTime = (rule, value, callback) => {
				if (value) {
					// 1.不验证整数
					// if (!Number.isInteger(value * 1)) {
					// 	callback(new Error("请输入整数"));
					// } else {
					if (value * 1 < 0 || value >= 60) {
						callback(new Error("请输入正确的测试成绩"));
					} else {
						callback();
					}
					// }
				} else {
					callback(new Error("请输入测试成绩"));
				}
			};

			let checkTiaoyuan = (rule, value, callback) => {
				if (value) {
					if (!Number.isInteger(value)) {
						callback(new Error("请输入整数"));
					} else {
						callback();
					}
				} else {
					callback(new Error("请输入测试成绩"));
				}
			};

			return {
				tests: [],
				uid: "",
				scoreForm: {
					campusId: "",
					name: "",
					gender: "",
					grade: "",
					college: "",
					testId: "",
					height: "",
					weight: "",
					feihuoliang: "",
					shortRun: "",
					qianqu: "",
					up: "",
					longRun_min: "",
					longRun_sec: ""
				},
				rules: {
					campusId: [{ required: true, message: "请输入学号", trigger: "blur" }],
					height: [{ validator: checkWeight }],
					weight: [{ validator: checkNumber }, { validator: checkWeight }],
					feihuoliang: [{ validator: checkInt }, { validator: checkFHL }],
					shortRun: [{ validator: checkNumber }, { validator: checkShortRun }],
					qianqu: [{ validator: checkQianqu }],
					tiaoyuan: [{ validator: checkNumber }, { validator: checkTiaoyuan }],
					longRun_min: [{ validator: checkInt }],
					longRun_sec: [{ validator: checkTime }],
					up: [{ validator: checkInt }]
				}
			};
		},
		mounted() {
			this.getAllTest();
		},
		computed: mapState({ user: state => state.user }),
		methods: {
			//保存
			save() {
				let url = this.$util.domain + "/physicaltest/physicaltestscore/newscore";

				if (!this.scoreForm.testId) {
					return;
				}

				this.$refs.scoreForm.validate(valid => {
					if (valid) {
						let miliseconds = this.scoreForm.longRun_min * 60 * 1000 + this.scoreForm.longRun_sec * 1000;

						this.$http({
							url,
							method: "POST",
							body: JSON.stringify({
								run: miliseconds,
								uid: this.uid * 1,
								name: this.scoreForm.name,
								campusId: this.scoreForm.campusId,
								department: this.scoreForm.college,
								height: this.scoreForm.height * 10,
								upperAbdomen: this.scoreForm.up * 1,
								weight: this.scoreForm.weight * 1000,
								physicalId: this.scoreForm.testId * 1,
								run50: this.scoreForm.shortRun * 1000,
								sitandreach: this.scoreForm.qianqu * 10,
								gender: this.scoreForm.gender == "男" ? 1 : 0,
								vitalCapacity: this.scoreForm.feihuoliang * 1,
								standingLongJump: this.scoreForm.tiaoyuan * 10,
								grade: this.scoreForm.grade.replace("级", "") * 1
							})
						}).then(response => {
							if (response.data.error == 10000) {
								this.$util.successMsg("保存成功");
								this.cancel();
							} else {
								this.$util.errorMsg(response.data.message);
							}
						});
					}
				});
			},
			//取消
			cancel() {
				let params = {
					physicalId: this.$util.getUrlParam("physicalId"),
					grade: this.$util.getUrlParam("grade"),
					college: this.$util.getUrlParam("college"),
					pageNum: this.$util.getUrlParam("pageNum"),
					keyword: this.$util.getUrlParam("keyword")
				}
				this.$util.refreshTo("/#/test_scoreManage", params);
			},
			//获取所有体测项
			getAllTest() {
				let url = this.$util.domain + "/physicaltest/common/testitemlist";
				this.tests = [];

				this.$http({
					url,
					method: "POST"
				}).then(response => {
					if (response.data.error == 10000) {
						this.tests = response.data.data;
						if (this.tests && this.tests.length) {
							this.scoreForm.testId = this.tests[0].physicalId;
						}
					}
				});
			},
			//根据学号补全信息
			completeInfo() {
				let url = this.$util.domain + "/campusadminapi/v1/tools/studentinfobycampusid";

				if (this.scoreForm.campusId) {
					this.$http({
						url,
						method: "GET",
						params: {
							campusId: this.scoreForm.campusId + ""
						}
					}).then(response => {
						if (response.data.error == 10000) {
							let dataObj = response.data.data;
							if (dataObj) {
								this.uid = dataObj.uid;
								this.scoreForm.name = dataObj.name;
								this.scoreForm.grade = dataObj.grade + "级";
								this.scoreForm.college = dataObj.department;
								this.scoreForm.gender = dataObj.gender == 1 ? "男" : "女";
								this.getScoreDetail(dataObj.uid, this.scoreForm.campusId);
							} else {
								this.uid = "";
								this.scoreForm.name = "";
								this.scoreForm.grade = "";
								this.scoreForm.gender = "";
								this.scoreForm.college = "";
							}
						}
					});
				}
			},
			//获取已测学生成绩详情
			getScoreDetail(uid, campusId) {
				let url = this.$util.domain + "/physicaltest/physicaltestscoredetail";

				this.$http({
					url,
					method: "POST",
					body: JSON.stringify({
						physicalId: this.scoreForm.testId * 1,
						uid: uid * 1
					})
				}).then(response => {
					this.$refs.scoreForm.resetFields();
					this.scoreForm.campusId = campusId;
					if (response.data.error == 10000) {
						let dataObj = response.data.data;
						let longRunScore = this.timeFormat(dataObj.run);
						if (dataObj.height !== -99999) {
							this.scoreForm.height = dataObj.height / 10;
						}
						if (dataObj.weight !== -99999) {
							this.scoreForm.weight = dataObj.weight / 1000;
						}
						if (dataObj.vitalCapacity !== -99999) {
							this.scoreForm.feihuoliang = dataObj.vitalCapacity;
						}
						if (dataObj.run50 !== -99999) {
							this.scoreForm.shortRun = dataObj.run50 / 1000;
						}
						if (dataObj.sitAndReach !== -99999) {
							this.scoreForm.qianqu = dataObj.sitAndReach / 10;
						}
						if (dataObj.standingLongJump !== -99999) {
							this.scoreForm.tiaoyuan = dataObj.standingLongJump / 10;
						}
						if (dataObj.upperAbdomen !== -99999) {
							this.scoreForm.up = dataObj.upperAbdomen;
						}
						if (dataObj.run !== -99999) {
							this.scoreForm.longRun_min = longRunScore.minute;
							this.scoreForm.longRun_sec = longRunScore.second;
						}
					}
				});
			},
			//转化时间
			timeFormat(time) {
				let int = parseInt(time / 60000);
				let float = Math.ceil((time / 1000 - 60 * int) * 10) / 10;
				let newTime = int + "'" + float + '"';

				return {
					time: newTime,
					minute: int,
					second: float
				};
			}
		}
	};
</script>

<style scoped>
	#newScore {
		padding-top: 80px;
		height: calc(100% - 100px);
	}

	.title {
		color: #0398ff;
	}

	#newScore .mytitle {
		color: #48576a;
		margin-bottom: 10px;
	}

	#newScore .el-input {
		display: inline-block;
		width: 250px;
	}

	#newScore .mytime {
		width: 118px;
	}

	#newScore .el-form-item {
		color: #48576a;
	}
</style>